<template>
	<view class="route-quicksale">
		<view class="cont">
			<view class="inner">
				<view class="have" v-if="goodsList.length">
					<view class="item" v-for="(item,index) in goodsList" :key="index">
						<view class="item-top">
							<view class="top-left">
								<image class="img" :src="item.image.split(',')[0]" mode="aspectFill"></image>
							</view>
							<view class="top-right">
								<view class="name">
									{{item.title}}
								</view>
								<view class="numcont">
									<view class="citem">
										<view class="label">
											查看次数：
										</view>
										<view class="val">
											{{item.seenum}}
										</view>
									</view>
									<view class="citem">
										<view class="label">
											购买次数：
										</view>
										<view class="val">
											{{item.paynum}}
										</view>
									</view>
								</view>
								<view class="price">
									￥{{item.price}}
								</view>
							</view>
						</view>
						<view class="item-footer">
							<view v-if="item.bianji==1" class="text-btn color1" @click="handleEdit(item)">
								编辑
							</view>
							<view class="text-btn color2" @click="handleDel(item)">
								删除
							</view>
							<button @click="handleShare(item)" class="text-btn color3">
								分享
							</button>
						</view>
					</view>
					<u-loadmore :status="status" />
				</view>
				<view class="none" v-else>
					<u-empty text="暂无商品" mode="list"></u-empty>
				</view>

			</view>
		</view>
		<view class="weekNotice" v-if="showWeekNotice">
			<view class="">
				你的试用额度即将用完
			</view>
			<view class="btncont">
				<u-button v-if="!isIos" type="primary" size="mini" text="续费" @click="handleShowAndrRenw"></u-button>
				<u-button v-if="isIos" type="primary" size="mini" text="联系客服" @click="handleKefu"></u-button>
			</view>
		</view>
		<view class="footer">
			<view class="btn def-bgcol rowbox " @click="handleGoAdd">
				添加快卖商品
			</view>
		</view>
		<IosKefu :show="showIosKefu" :type="matype" @close="closeIosKefu"></IosKefu>
		<AndrTaocan :show="showAndrTaocan" :taocanid="5" @close="closeAndrTaocan" @refsh="handlerefsh"></AndrTaocan>
	</view>
</template>

<script>
	import IosKefu from '@/components/ioskefu.vue'
	import AndrTaocan from '@/components/andrtaocan.vue'
	export default {
		components: {
			IosKefu,
			AndrTaocan
		},
		data() {
			return {
				showAndrTaocan: false,
				matype: '',
				showIosKefu: false,
				isIos: false,
				showWeekNotice: false,
				userinfo: null,
				status: 'nomore',
				goodsList: [],
				totalPage: 0,
				currentPage: 1,
			}
		},
		onShow() {
			const info = wx.getSystemInfoSync()
			this.isIos = !(info.system.indexOf('Android') > -1)
			if (uni.getStorageSync('uid')) {
				this.userinfo = uni.getStorageSync('userInfo')
				this.getDataList()

			}
			// this.getStrorInfo()
		},
		onReachBottom() {
			console.log(3333)
			this.loadData()
		},


		methods: {
			handlerefsh() {
				// this.getStrorInfo()
			},
			handleShowAndrRenw() {
				this.showAndrTaocan = true
			},
			closeAndrTaocan() {
				this.showAndrTaocan = false
			},
			handleKefu() {
				this.showIosKefu = true
				this.matype = 'dpkmimg'
			},
			closeIosKefu() {
				this.showIosKefu = false
			},
			getStrorInfo() {
				this.$req.post('/shanghu/shanghuuserInfo', {
					uid: uni.getStorageSync('uid')
				}).then((res) => {
					let data = res.data.data
					if (data.dpkm === 1 && data.dpkminttime === 0) {
						if (wx.getSystemInfoSync().system.indexOf('Android') > -1) {
							// uni.navigateTo({
							// 	url: '/pages/buy/buy?id=' + 5
							// })
							this.showAndrTaocan = true
						} else {
							this.matype = 'dpkmimg'
							this.showIosKefu = true
						}
					} else if (data.dpkm === 1 && data.dpkminttime > 0) {
						if (wx.getSystemInfoSync().system.indexOf('Android') > -1) {
							// uni.navigateTo({
							// 	url: '/pages/renew/renew?id=' + 5
							// })
							this.showAndrTaocan = true
						} else {
							this.matype = 'dpkmimg'
							this.showIosKefu = true
						}
					} else {
						if (data.dpkmnum < 10) {
							this.showWeekNotice = true
						}
						if (data.dpkmnum < 2) {
							let flag = true
							if (uni.getStorageSync('dpkmtaoendtime')) {
								var ctime = new Date().getTime()
								if (ctime > uni.getStorageSync('dpkmtaoendtime')) {
									uni.removeStorageSync('dpkmtaoendtime')
								} else {
									flag = false
								}
							}
							if (this.isIos) {
								this.matype = 'dpkmimg'
								this.showIosKefu = flag
							} else {
								this.showAndrTaocan = flag
							}

							if (!uni.getStorageSync('dpkmtaoendtime')) {
								let endTime = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 *
									60 *
									60 *
									1000 - 1); // 当天23:59
								uni.setStorageSync('dpkmtaoendtime', endTime)
							}
						}
					}
				})
			},
			getDataList() {
				this.currentPage = 1
				uni.showLoading({
					title: '加载中'
				})
				this.$req.post('/shanghu/danpinlist', {
					shanghuid: this.userinfo.shanghuid || '',
					pageNo: this.currentPage,
					pageSize: 20
				}).then(res => {
					uni.hideLoading()
					let info = res.data
					console.log(info.data, 9999)
					this.goodsList = info.data.data || []
					this.totalPage = info.data.totalPage
					if (this.totalPage <= this.currentPage) {
						this.status = 'nomore'
					}
				})
			},
			loadData() {
				console.log(this.currentPage, this.totalPage)
				if (this.currentPage >= this.totalPage) return;
				this.status = 'loading'
				this.currentPage = ++this.currentPage
				let req = {
					shanghuid: this.userinfo.shanghuid || '',
					pageNo: this.currentPage,
					pageSize: 20
				}
				uni.showLoading({
					title: '加载中'
				})
				this.$req.post('/shanghu/danpinlist', req).then((res) => {
					uni.hideLoading()
					let info = res.data
					this.goodsList = [...this.goodsList, ...info.data.data]

					if (this.currentPage >= this.totalPage) {
						this.status = 'nomore'
					}
				})

			},
			handleGoAdd() {
				uni.navigateTo({
					url: '/pages/quicksaleedit/quicksaleListAdd'
				})
			},
			handleEdit(data) {
				uni.navigateTo({
					url: '/pages/quicksaleedit/quicksaleListAdd?id=' + data.id
				})
			},
			handleDel(data) {

				uni.showModal({
					title: '提示', // 标题
					content: '确定删除当前单品吗?', // 提示内容
					labelColor: '#3385e3', // 确认按钮文字颜色
					cancelText: "取消", // 取消按钮的文字
					confirmText: "确认", // 确认按钮的文字
					showCancel: true, // 是否显示取消按钮，默认为 true
					confirmColor: '#ED2920',
					success: (res) => {
						if (res.confirm) {
							//点击确定之后执行的代码
							uni.showLoading({
								mask: true,
							})
							this.$req.post('/shanghu/danpindel', {
								id: data.id
							}).then((res) => {
								uni.hideLoading()
								if (res.data.code === 0) {
									uni.showToast({
										mask: true,
										icon: 'none',
										title: '删除成功'
									})
									this.getDataList()
								} else {
									uni.showToast({
										mask: true,
										icon: 'none',
										title: res.data.msg
									})
								}
							})
						} else {
							//点击取消之后执行的代码
						}
					}
				});
			},
			handleShare(data) {
				uni.navigateToMiniProgram({
					appId: 'wx42f52aeb0203685a',
					path: `/pages/quicksale/quicksale?id=${data.id}&shanghuid=${this.userinfo.shanghuid}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.weekNotice {
		padding: 15rpx 30rpx;
		position: fixed;
		bottom: 120rpx;
		left: 0;
		z-index: 100;
		width: 100%;
		background-color: #fdf6ec;
		color: #f9ae3d;
		font-size: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.route-quicksale {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-bottom: 200rpx;



		.cont {
			padding: 30rpx 24rpx 0 24rpx;

			.inner {
				.have {
					.item {
						background-color: #fff;
						padding: 20rpx;
						border-radius: 8rpx;
						margin-bottom: 30rpx;

						.item-top {
							display: flex;

							.top-left {
								flex-shrink: 0;
								width: 190rpx;
								height: 190rpx;
								border-radius: 8rpx;
								overflow: hidden;

								.img {
									display: block;
									width: 100%;
									height: 100%;
								}
							}

							.numcont {
								display: flex;
								padding-top: 20rpx;

								.citem {
									display: flex;
									margin-right: 30rpx;
									font-size: 26rpx;

									.label {
										color: #999;
									}
								}
							}

							.top-right {
								flex: 1;
								padding-left: 40rpx;

								.name {
									font-size: 32rpx;
									font-weight: 500;
								}

								.price {
									font-size: 36rpx;
									margin-top: 30rpx;
									color: #ED2920;
								}
							}
						}

						.item-footer {
							display: flex;
							justify-content: flex-end;

							button {
								padding: 0;
								margin: 0;
								line-height: 38rpx;
							}

							button::after {
								border: none;
							}

							.text-btn {

								// margin-left: 10rpx;

								background-color: transparent;
								padding: 10rpx 20rpx;
								font-size: 28rpx;
							}

							.color1 {
								color: #2979ff;
							}

							.color2 {
								color: #fa3534;
							}

							.color3 {
								color: #19be6b;
							}
						}

					}
				}

				.none {
					padding-top: 10vh;
				}
			}
		}

		.footer {
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 10;
			width: 100%;
			background: #fff;
			padding: 10rpx 20rpx 0 20rpx;
			height: 120rpx;
			box-shadow: 0 0 4px 2px #eeecef;
			box-sizing: border-box;

			.btn {
				width: 100%;
				font-size: 28rpx;
				height: 90rpx;
				border-radius: 20rpx;
				color: #fff;
			}
		}
	}
</style>